import { Order, Page, Topup, User, type PageIn, type TinyUser } from '../client'
import { makeRequest } from '../client-core'
import { OrderIn, TopupIn } from './models'

const request = makeRequest('/admin/api/v1')

export class Admins extends request() {
  static list(params: PageIn) {
    return Admins.$api.get<Page<User>>({
      params,
      transform: (data) => Page.inst<any>(data, [User])
    })
  }

  static retrieve(id: number) {
    return Admins.$api.get<User>({
      paths: [id],
      typeof: User
    })
  }

  static me() {
    return Admins.$api.get<User>({ sub: '/me', typeof: User })
  }

  static create(data: User) {
    return Admins.$api.post({
      data,
      includes: ['username', 'password', 'name', 'avatar', 'role']
    })
  }

  static update(data: User) {
    return Admins.$api.put({
      paths: [data.id],
      data,
      includes: ['username', 'password', 'name', 'avatar', 'role']
    })
  }

  static destroy(id: number) {
    return Admins.$api.delete({ paths: [id] })
  }
}

export class Copilots extends request('/copilot') {
  static list(params?: PageIn) {
    return Copilots.$api.get<Page<User>>({
      params,
      transform: (data) => Page.inst<any>(data, [User])
    })
  }

  static tinylist() {
    return Copilots.$api.get<TinyUser[]>({
      sub: `/tinylist`
    })
  }

  static retrieve(id: number) {
    return Copilots.$api.get<User>({ paths: [id], typeof: User })
  }

  static create(data: User) {
    return Copilots.$api.post({
      data,
      includes: ['username', 'password', 'name', 'avatar', 'description', 'gender', 'phone', 'qrcode']
    })
  }

  static update(data: User) {
    return Copilots.$api.put({
      paths: [data.id],
      data,
      includes: ['username', 'password', 'name', 'avatar', 'description', 'gender', 'phone', 'qrcode']
    })
  }

  static destroy(id: number) {
    return Copilots.$api.delete({ sub: `/${id}` })
  }
}

export class Users extends request('/user') {
  static list(params: PageIn) {
    return Users.$api.get<Page<User>>({ params, transform: (data) => Page.inst<any>(data, [User]) })
  }

  static tinylist() {
    return Users.$api.get<TinyUser[]>({ sub: `/tinylist` })
  }

  static retrieve(id: number) {
    return Users.$api.get<User>({ paths: [id], typeof: User })
  }

  static create(data: User) {
    return Users.$api.post({
      data,
      includes: ['username', 'password', 'name', 'avatar', 'amount', 'balance']
    })
  }

  static update(data: User) {
    return Users.$api.put({
      paths: [data.id],
      data,
      includes: ['username', 'password', 'name', 'avatar', 'amount', 'balance']
    })
  }

  static destroy(id: number) {
    return Users.$api.delete({ paths: [id] })
  }
}

export class Topups extends request('/topup') {
  static list(params: PageIn) {
    return Topups.$api.get<Page<Topup>>({ params, transform: (data) => Page.inst<any>(data, [Topup]) })
  }

  static retrieve(id: number) {
    return Topups.$api.get<Topup>({ paths: [id], typeof: Topup })
  }

  static create(data: TopupIn) {
    return Topups.$api.post({ data })
  }

  static update(data: Topup) {
    return Topups.$api.put({ paths: [data.id], data, includes: ['user', 'description', 'amount', 'gift'] })
  }

  static paid(id: number) {
    return Topups.$api.patch({ sub: `/paid`, paths: [id] })
  }

  static destroy(id: number) {
    return Topups.$api.delete({ paths: [id] })
  }
}

export class Orders extends request('/order') {
  static list(params: PageIn) {
    return Orders.$api.get<Page<Order>>({ params, transform: (data) => Page.inst<any>(data, [Order]) })
  }

  static retrieve(id: number) {
    return Orders.$api.get<Order>({ paths: [id], typeof: Order })
  }

  static create(data: OrderIn) {
    return Orders.$api.post({ data })
  }

  static update(data: Order) {
    return Orders.$api.put({
      paths: [data.id],
      data,
      includes: ['user', 'copilot', 'description', 'type', 'price', 'duration']
    })
  }

  static paid(id: number) {
    return Orders.$api.patch({ sub: '/paid', paths: [id] })
  }

  static destroy(id: number) {
    return Orders.$api.delete({ sub: `/${id}` })
  }
}

export class Logs extends request('/log') {
  static list(params: PageIn) {
    return Logs.$api.get<Page<string>>({ params, transform: (data) => Page.inst<any>(data) })
  }
}
